package com.hj.mybatis.service.impl;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hj.mybatis.query.BaseQuery;
import com.hj.mybatis.service.BaseService;


/**
 * @author hj
 * @Date 2023/4/26
 * @Description: 基础服务接口实现类
 */
public class BaseServiceImpl<M extends BaseMapper<T>, T> extends ServiceImpl<M, T> implements BaseService<T> {


    /**
     * 获取分页对象
     *
     * @param query 分页参数
     */
    protected IPage<T> getPage(BaseQuery query) {
        Page<T> page = new Page<>(query.getPage(), query.getPageSize());
        // 排序
        if (StringUtils.isNotBlank(query.getOrderField())) {
            if (query.isAsc()) {
                return page.addOrder(OrderItem.asc(query.getOrderField()));
            } else {
                return page.addOrder(OrderItem.desc(query.getOrderField()));
            }
        }
        return page;
    }



}